fix: try and preserve Double and Long/Int64 types in the json editor COMPASS-8337 #7119
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the JSON editor ie. the middle one of the three in the Documents tab. Suppose you have this document:
and you edit it in the JSON editor, it gets serialised as
{ "a": 1, "b": 1, "object": { "c": 1, "d": 1 } }so when you save that back it becomes
ie. we implicitly change the type of the Double or Long into an Int32 because 1 can safely be represented by a 32bit int.
This change will check the type of a property and if it is now Int32 and it was Double or Long, it will turn it back into a Double or Long when you save. So the document keeps its original "unnecessary" type.
Just to be clear: This only applies to the json editor. The other editors are unaffected.
In case you're wondering why this is broken in the json editor but not in the document editor:
The reason why we lose the type information is because it isn't encoded in the JSON and then we do HadronDocument.FromEJSON(value || '') which just re-infers all the types using TypeChecker.type(). TypeChecker.type() will use
_bsontypeif it is set, otherwise it does this thing where it tries to figure out if it should be a double or an int.Because it uses
_bsontypeif set and because the double and Int64 editors explicitly cast the type, the exact type is never lost in the document editor.I decided to recurse on objects, but leave arrays alone. It is really tricky and error-prone to detect if the user's intention was to have the whole array use Long and Double and never Int32 or not. Although it would have potentially been useful for embeddings / vectors where the values just might end up as a round number in an array that is otherwise doubles. I tried a few things but gave up on that idea every time because it just felt inherently dangerous. We could re-visit the idea in the future if necessary. Follow-up ticket: https://jira.mongodb.org/browse/COMPASS-9578